#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Solution {
    public:
	int minimumTime(int n, vector<vector<int>> &relations, vector<int> &time)
	{
                vector<int> wait(n + 1, 0);
                vector<int> head(n + 1, 0);
                vector<int> next(relations.size() + 1, 0);
                vector<int> to(relations.size() + 1, 0);
                vector<int> indegree(n + 1, 0);
                int cnt = 1;

                for(auto e : relations){
                        next[cnt] = head[e[0]];
                        to[cnt] = e[1];
                        head[e[0]] = cnt++;
                        indegree[e[1]]++;
                }

                queue<int> q;
                for(int i = 1; i <= n; i++){
                        if(indegree[i] == 0){
                                q.push(i);
                        }
                }

                int ans = 0;
                time.insert(time.begin(), 0);
                while(!q.empty()){
                        int u = q.front();
                        q.pop();
                        ans = max(ans,  wait[u] + time[u]);

                        for(int e = head[u]; e; e = next[e]){
                                int v = to[e];
                                wait[v]= max(wait[v],  wait[u] + time[u]);
                                if(--indegree[v] == 0){
                                        q.push(v);
                                }
                        }
                }
                return ans;
	}
};

int main()
{
        int n = 3;
        vector<vector<int>> relations = {{1,3},{2,3}};
        vector<int> time = {3,2,5};
        cout << Solution().minimumTime(n, relations, time)<<endl;

        return 0;
}